/**
 * 
 */
package com.kunbo.cn.service.impl.core.sale;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kunbo.cn.dao.core.sale.SaleOrderQuantityAdjustDao;
import com.kunbo.cn.entity.core.sale.SaleOrderQuantityAdjust;
import com.kunbo.cn.service.core.sale.SaleOrderQuantityAdjustService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.Pages;
/**
 *
 * @author bjl  
 * @date 2019年4月18日 
 */
@Service
public class SaleOrderQuantityAdjustServiceImpl implements SaleOrderQuantityAdjustService {
	@Autowired
	private SaleOrderQuantityAdjustDao dao;
	@Autowired
    private JdbcTemplate jdbcTemplate;
	
	@Override
	public void save(SaleOrderQuantityAdjust soqa) {
		soqa.setCreate_time(SDate.getSystemDateToString());
		dao.save(soqa);
	}

	@Override
	public List<Map<String, Object>> list(String order_code) {
		String sql = "SELECT * FROM business_sale_order_quantity_adjust s where 1=1 and s.sale_order_code='"+order_code+"'  order by s.create_time desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@Override
	public List<Map<String, Object>> OrderList() {
		String sql = "SELECT s.id,s.uuid,s.sale_order_code,s.name ,s.customer ,s.materials,s.contract_all_amount,s.lower_amount,s.contr_margin,s.supplier FROM business_sale_order_manage s where 1=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);//and p.state <> 5 and p.state <> 6
		return list;
	}
	@Override
	public Pages OrderList1(Integer page,Integer pageSize) {
		String sql1 = "SELECT s.id,s.uuid,s.sale_order_code,s.name ,s.customer ,s.materials,s.contract_all_amount,s.lower_amount,s.contr_margin,s.supplier FROM business_sale_order_manage s where 1=1";
		String sql2="";
		sql2 += " order by s.id  desc  limit " + (page * pageSize) +"," + pageSize;
		  String sql=sql1.concat(sql2);//获取数据sql
		  List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		  Pages pages =new Pages();
		  Integer count = jdbcTemplate.queryForList(sql1).size();
		  pages.setTotal(count);
		  pages.setData(list);
		return pages;
	}
	/**
	 *
	 * 1.修改销售订单余量结转后订单余量(包括调量订单,调入订单余量)
	 * 2.先判断调量，调入订单号是否为空；
	 * 3.查询两个订单号两种材料是否一致
	 * 4.查询调量订单余量，查询调入订单余量，判断调量订单余量是否大于调整量
	 * 5.执行调量
	 * 6.先执行调量再保存调转订单
	 * 7.保证单一修改事务的回滚;
	 * 
	 */
	@Override
	public int AdjustOrderNum(String sale_order_code,String contract_all_amount,String lower_amount,String adjust_amount,String contr_margin ) throws SQLException {
		int n=0; int i=0;int n1=0;
		  //if((sale_order_code!=null && !adjust_amount.isEmpty())&&(newOrder!=null && !newOrder.isEmpty())) {
		Float contract_all_amounts =Float.valueOf(contract_all_amount);
		Float lower_amounts =Float.valueOf(lower_amount);
		Float adjust_amounts =Float.valueOf(adjust_amount);
		Float contr_margins =Float.valueOf(contr_margin);
		Float new_contract_all_amount=contract_all_amounts+adjust_amounts;
		Float new_lower_amount=lower_amounts+adjust_amounts;
		Float new_contr_margin= contr_margins+adjust_amounts;
		if((sale_order_code!=null && !adjust_amount.isEmpty())){
		//1.修改调量订单(先查询余量)
			String sql = "UPDATE \r\n" +
	        "business_sale_order_manage d \r\n"+
	        " set d.contr_margin="+new_contr_margin+"\r\n "+
	        "  ,d.lower_amount="+new_lower_amount+"\r\n "+
	        "  ,d.contract_all_amount="+new_contract_all_amount+"\r\n "+
	        " WHERE  d.sale_order_code='"+sale_order_code+"'";
			 i=jdbcTemplate.update(sql);
	}
		return i;
	}
}
